/** @file GOTHspiEeprom.h This file contains the source code for the SPI-EEPROM API of the GOTH
 * Kick-ass Etherboard.
 * 
 * This API contains all commands to control, read from and write to the EEPROM.
 *
 * @author Geoffrey Ottoy - Tom Hamelinckx
 * @date 05/07/2011 (last modified)
 *
 * <pre>
 * MODIFICATION HISTORY:
 *
 * Who  Date     Changes
 * ---- -------- ---------------------------------------------------------
 * TH   04/07/11 Created this file.
 * TH	05/07/11 Finalized, tested and documented
 * </pre>
 * 
 */
#ifndef __GOTH_SPI_EEPROM_H__
#define __GOTH_SPI_EEPROM_H__

#include "p24fj256GB110.h"
#include "GOTHspi.h"
#include "GOTHglobal.h"

#define SPI_SelectEeprom() {LATBbits.LATB8 = 0;}
#define SPI_DeselectEeprom() {LATBbits.LATB8 = 1;}
 
//EEPROM instruction set
#define EEPROM_CMD_READ     (unsigned)0b00000011
#define EEPROM_CMD_WRITE    (unsigned)0b00000010
#define EEPROM_CMD_WRDI     (unsigned)0b00000100
#define EEPROM_CMD_WREN     (unsigned)0b00000110
#define EEPROM_CMD_RDSR     (unsigned)0b00000101
#define EEPROM_CMD_WRSR     (unsigned)0b00000001


///
///Enum for EEPROM_STATUS
///Contains all possible status-responses for the EEPROM
///
enum EEPROM_STATUS { EEPROM_PROTECT_QUART= 4, EEPROM_PROTECT_HALF=8, EEPROM_PROTECT_FULL=12, EEPROM_SUCCESS, EEPROM_FAILURE, EEPROM_IDLE, EEPROM_PAGE_OVERFLOW, EEPROM_BUSY};

typedef int EEPROM_STATUS;
/** Initialize EEPROM for operation.
 * 
 * @return	nothing
 *
 */
void SPI_InitEeprom(void);

/** Read the EEPROM's Status Register
 * Only the lowest nibble of the byte contains valid information.
 * Bit 2,3: containts Write Protected Area information
 * Bit 1  : Flags the Write Enable Latch
 * Bit 0  : Flags Write In Progress 
 * 
 * @return	The Status Register Byte.
 *
 */
unsigned char SPI_ReadSR(void);

/** Read what areas are protected in the EEPROM
 * 
 * @return The status of the protection
 */
EEPROM_STATUS SPI_EepromGetProtectionLevel(void);

/** Change what areas are to be protected in the EEPROM
 * 
 * The user is not allowed to unprotect the area where the MAC address is stored!
 *
 * @param	status	The status (from the EEPROM_STATUS enum) to be written
 *					This can be:
 *						- EEPROM_PROTECT_QUART: Protect upper quarter (always ON!)
 *						- EEPROM_PROTECT_HALF : Protect upper half
 *						- EEPROM_PROTECT_FULL : Protect ALL
 *
 */
void SPI_EepromChangeWriteProtection(EEPROM_STATUS status);

/** Checks if the EEPROM is busy or not
 * 
 * @return	The status information (EEPROM_IDLE or EEPROM_BUSY)
 *
 */
EEPROM_STATUS SPI_EepromIsBusy(void);

/** Write an array of data to the EEPROM
 *
 * It is possible to write one single page in one write cycle. This implies a maximum of 16 bytes 
 * per cycle.
 * The function will return an EEPROM_PAGE_OVERFLOW error if you try to write to much data.
 * The function will return an EEPROM_BUSY error if the device is still writing the previous data.
 *
 * @param	address	The address where to write. Everything possible from 0x00 to 0xBF.
 * @param	buffer	The data that has to be stored
 * @param	length	The length of the data array to be stored.
 *
 * @return	The status information (EEPROM_PAGE_OVERFLOW or EEPROM_BUSY or EEPROM_SUCCES)
 *
 */
EEPROM_STATUS SPI_WriteEeprom(unsigned char address, unsigned char * buffer, unsigned char length);

/** Read an array of data to the EEPROM
 *
 * The function will return an EEPROM_BUSY error if the device is still writing.
 *
 * @param	address	The address where to read from. Everything possible from 0x00 to 0xFF.
 * @param	buffer	The databuffer to put the data in.
 * @param	length	The length of the data to be stored.
 *
 * @return	The status information (EEPROM_BUSY or EEPROM_SUCCES)
 *
 */
EEPROM_STATUS SPI_ReadEeprom(unsigned char address, unsigned char * buffer, unsigned char length);

/** Get the MAC address stored in the EEPROM
 *
 * The function will return an EEPROM_BUSY error if the device is still writing.
 *
 * @param	MACBuffer	The databuffer to put the MAC address in
 * 
 * @return	The status information (EEPROM_BUSY or EEPROM_SUCCESS)
 */
EEPROM_STATUS SPI_getMAC(unsigned char * MACBuffer);
 
 
 
#endif

